﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for MemberList
    /// </summary>
    public class MemberList
    {
        private int _MemberListID;

        public int MemberListID
        {
            get
            {
                return _MemberListID;
            }
            set
            {
                _MemberListID = value;
            }
        }
        private int _GroupID;

        public int GroupID
        {
            get
            {
                return _GroupID;
            }
            set
            {
                _GroupID = value;
            }
        }
        private String _Description;

        public String Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }

        public MemberList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public MemberList(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _MemberListID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _GroupID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _Description = SqlReader.GetString(2);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "MemberList_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static MemberList GetInfo(int MemberListID)
        {
            MemberList member = new MemberList();
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@member_list_id", MemberListID);
            string cmd = "MemberList_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                member.MemberListID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    member.GroupID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    member.Description = SqlReader.GetString(2);
                }
            }
            return member;
        }

        /// <summary>
        /// Get MemberList of Group
        /// </summary>
        /// <param name="GroupID"></param>
        /// <returns></returns>
        public static int GetMemberListID(int GroupID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@group_id", GroupID);
            String cmd = @"select * from tbl_member_list where group_id = @group_id";
            DataTable dt = Dbh.ExecuteDataTable(cmd, CommandType.Text);
            if (dt.Rows.Count == 0)
                return -1;
            else
                return (int)dt.Rows[0]["member_list_id"];
        }

        public static int Insert(MemberList member)
        {
            DBHelp Dbh = new DBHelp();

            SqlParameter MemberListID = new SqlParameter();
            MemberListID.DbType = DbType.Int32;
            MemberListID.Direction = ParameterDirection.Output;
            MemberListID.ParameterName = @"@member_list_id";
            Dbh.AddParameter(MemberListID);
            Dbh.AddParameter("@group_id", member.GroupID);
            Dbh.AddParameter("@description", member.Description);
            try
            {
                string cmd = "MemberList_Insert";
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)MemberListID.Value;
            }
            catch (System.Exception e)
            {
                return -1;
            }
        }

        public static void Update(MemberList member)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@member_list_id", member.MemberListID);
            Dbh.AddParameter("@group_id", member.GroupID);
            Dbh.AddParameter("@description", member.Description);
            string cmd = "MemberList_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int MemberListID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@member_list_id", MemberListID);
            string cmd = "MemberList_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    }
}